package com.cheer.ba.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.footview.CustomerFooter;
import com.andview.refreshview.smileyloadingview.SmileyHeaderView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cheer.ba.R;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.OilGoodsInfoModel;
import com.cheer.ba.presenter.impl.FindOilPresenter;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.activity.FindOilAty;
import com.cheer.ba.view.activity.OilDetailAty;
import com.cheer.ba.view.activity.SearchOilAty;
import com.cheer.ba.view.adapter.FindOilAdp;
import com.cheer.ba.view.animation.CustomAnimation;
import com.cheer.ba.view.base.BaseCommFrg;
import com.cheer.ba.widget.headview.CheerHeadView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 柴油批发菜单栏
 */
public class OilMenuFrg extends BaseCommFrg {

    @BindView(R.id.id_oil_sale_txt)
    TextView mTxtSaleNum;
    //-1表示不排序 1排序
    private String mSaleSort;
    private boolean isDirection_sale = false;

    @BindView(R.id.id_oil_sale_price_txt)
    TextView mTxtPrice;
    //-1
    private String mPriceSort;
    private boolean isDirection_price = false;

    //没有数据
    @BindView(R.id.id_error_data_txt)
    TextView mTxtHadNoData;

    @BindView(R.id.id_find_oil_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_find_oil_recyclev)
    RecyclerView mRecycleView;
    private FindOilAdp mFindOilAdp;
    //存储柴油信息
    private List<OilGoodsInfoModel> mOilGoodInfos = new ArrayList<OilGoodsInfoModel>();

    //地区
    private String mOilArea;
    //种类
    private String mOilType;
    //油源
    private String mOilOriginal;
    //油的标准
    private String mOilRule;

    private FindOilAty mFindOilAty;

    public void setFindOilAty(FindOilAty mFindOilAty) {
        this.mFindOilAty = mFindOilAty;
    }

    public static final OilMenuFrg newInstance(String mArea, String oilType, String oilOriginal, String oilRule) {
        OilMenuFrg frg = new OilMenuFrg();
        Bundle bundle = new Bundle();
        bundle.putString(IntentKey.FindOil.OIL_AREA, mArea);
        bundle.putString(IntentKey.FindOil.OIL_TYPE, oilType);
        bundle.putString(IntentKey.FindOil.OIL_ORIGINAL, oilOriginal);
        bundle.putString(IntentKey.FindOil.OIL_RULE, oilRule);
        frg.setArguments(bundle);
        return frg;
    }

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.frg_drawer_oil_content, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new FindOilPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return !mOilGoodInfos.isEmpty();
    }

    @Override
    public void initView() {
        mOilArea = getArguments().getString(IntentKey.FindOil.OIL_AREA);
        mOilType = getArguments().getString(IntentKey.FindOil.OIL_TYPE);
        mOilOriginal = getArguments().getString(IntentKey.FindOil.OIL_ORIGINAL);
        mOilRule = getArguments().getString(IntentKey.FindOil.OIL_RULE);

        mTxtSaleNum.setSelected(false);
        mTxtPrice.setSelected(false);
        mSaleSort = "-1";
        mPriceSort = "-1";

        setXRefreshView(mXRefreshView, true, true, false, true, new CheerHeadView(frg), new CustomerFooter(frg));
        mRecycleView.setLayoutManager(new LinearLayoutManager(frg));
        mRecycleView.setHasFixedSize(true);
        mRecycleView.setNestedScrollingEnabled(false);
        mFindOilAdp = new FindOilAdp(R.layout.adp_find_oil, mOilGoodInfos);
//        mFindOilAdp.openLoadAnimation(new CustomAnimation());
        mFindOilAdp.setNotDoAnimationCount(3);
        mFindOilAdp.isFirstOnly(false);
        mRecycleView.setAdapter(mFindOilAdp);
    }

    @Override
    public void initData() {
        if (hasDataInPage()) {
            mXRefreshView.startRefresh();
        } else {
            loadData(!hasDataInPage(), false);
        }
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                loadData(false, true);
            }
        });
        mFindOilAdp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OilGoodsInfoModel model = (OilGoodsInfoModel) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.id_oil_company_txt:
                        //进入公司列表
                        Intent intent = new Intent(frg, SearchOilAty.class);
                        intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_OILMENU);
                        intent.putExtra(IntentKey.General.KEY_DES, model.orgNickName);
                        showActivity(frg, intent);
                        break;
                    case R.id.id_jump_detail_linear:
                        //进入详情
                        Intent details = new Intent(frg, OilDetailAty.class);
                        details.putExtra(IntentKey.General.KEY_ID, model.uid);
                        showActivity(frg, details);
                        break;
                }
            }
        });
    }

    /**
     * 加载数据
     *
     * @param loadMore 是否加载更多
     */
    public void loadData(boolean isShowLoad, boolean loadMore) {
        ((FindOilPresenter) presenter).findOils(isShowLoad, loadMore, mOilArea, mOilType, mOilOriginal, mOilRule, mSaleSort, mPriceSort, "");
        if (!loadMore) {
            stopRefresh(mXRefreshView, 2000);
        } else {
            stopLoadMore(mXRefreshView, 2000);
        }
    }

    /**
     * 排序
     *
     * @param isFirstSort true销量 false价格
     */
    public void setListSort(boolean isFirstSort) {
        if (isFirstSort) {
            isDirection_price = false;
        } else {
            isDirection_sale = false;
        }
        mTxtSaleNum.setSelected(isDirection_sale);
        mSaleSort = isDirection_sale ? "1" : "-1";

        mTxtPrice.setSelected(isDirection_price);
        mSaleSort = isDirection_price ? "1" : "-1";
    }

    @OnClick({R.id.id_oil_distance_linear, R.id.id_oil_price_linear, R.id.id_choose_txt})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //销量
            case R.id.id_oil_distance_linear:
                if (!isDirection_sale) {
                    isDirection_sale = true;
                } else {
                    isDirection_sale = false;
                }
                setListSort(true);
//                loadData(!hasDataInPage(), false);
                mXRefreshView.startRefresh();
                break;
            //价格
            case R.id.id_oil_price_linear:
                if (!isDirection_price) {
                    isDirection_price = true;
                } else {
                    isDirection_price = false;
                }
                setListSort(false);
                mXRefreshView.startRefresh();
//                loadData(!hasDataInPage(), false);
                break;
            //刷选
            case R.id.id_choose_txt:
                if (null != mFindOilAty) {
                    mFindOilAty.showDrawer();
                }
                break;
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_OIL_LIST)) {
                mRecycleView.setVisibility(View.VISIBLE);
                mTxtHadNoData.setVisibility(View.GONE);
                if (!loadMore) {
                    mOilGoodInfos.clear();
                    mXRefreshView.stopRefresh();
                } else {
                    mXRefreshView.stopLoadMore();
                }
                //柴油批发信息
                List<OilGoodsInfoModel> mOils = (List<OilGoodsInfoModel>) t;
                if (!mOils.isEmpty()) {
                    mOilGoodInfos.addAll(mOils);
                    mFindOilAdp.notifyDataSetChanged();
                } else {
                    if (loadMore) {
//                        mXRefreshView.setLoadComplete(true);
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        //没有数据
                        mRecycleView.setVisibility(View.GONE);
                        mTxtHadNoData.setVisibility(View.VISIBLE);
                        mTxtHadNoData.setText(getString(R.string.data_is_null));
                    }
                }
            }
        }
    }

    @Override
    public void handlerByPage(int flag1, int flag2, boolean loadMore) {
        mRecycleView.setVisibility(View.GONE);
        mTxtHadNoData.setVisibility(View.VISIBLE);
        if (flag2 == CodeStatus.Gegeneral.ERROR_NET) {
            mTxtHadNoData.setText(getString(R.string.no_net_work));
        } else if (flag2 == CodeStatus.Gegeneral.ERROR_DATA) {
            mTxtHadNoData.setText("数据加载失败，请检查网络是否链接");
        }
    }
}
